import pymysql,config

class DB:
    def __init__(self):
        self.__db = pymysql.connect(config.host,config.user,config.passwd,config.db, charset='utf8')
        self.__cur = self.__db.cursor()

    def __del__(self):
        self.commit()
        self.__cur.close()
        self.__db.close()

    def commit(self):
        try:
            self.__db.commit()
        except Exception as e:
            self.__db.rollback()
            print(e)

    def execute(self, sql,all=False,last=False):
        self.__cur.execute(sql)
        if last:
            return self.__cur.lastrowid
        if all:
            return self.__cur.fetchall()
        else:
            return self.__cur.fetchone()

